package com.example.hxds.mis.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.hxds.common.util.CosUtil;
import com.example.hxds.common.util.PageUtils;
import com.example.hxds.common.util.R;
import com.example.hxds.mis.api.controller.form.SearchDriverByPageForm;
import com.example.hxds.mis.api.controller.form.SearchDriverMonthBusinessDataForm;
import com.example.hxds.mis.api.controller.form.SearchDriverRealSummaryForm;
import com.example.hxds.mis.api.controller.form.UpdateDriverRealAuthForm;
import com.example.hxds.mis.api.feign.DrServiceApi;
import com.example.hxds.mis.api.feign.OdrServiceApi;
import com.example.hxds.mis.api.service.DriverService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class DriverServiceImpl implements DriverService {
    @Resource
    private DrServiceApi drServiceApi;
    @Autowired
    private OdrServiceApi odrServiceApi;
    @Autowired
    private CosUtil cosUtil;

    @Override
    public PageUtils searchDriverByPage(SearchDriverByPageForm form) {
        R r = drServiceApi.searchDriverByPage(form);
        HashMap map = (HashMap) r.get("result");
        PageUtils pageUtils = BeanUtil.toBean(map, PageUtils.class);
        return pageUtils;
    }

    @Override
    public HashMap searchDriverComprehensiveData(byte realAuth, Long driverId) {
        HashMap map = new HashMap();
        if (realAuth == 2 || realAuth == 3) {
            //查询司机实名信息摘要
            SearchDriverRealSummaryForm searchDriverRealSummaryForm = new SearchDriverRealSummaryForm();
            searchDriverRealSummaryForm.setDriverId(driverId);
            R r = drServiceApi.searchDriverRealSummary(searchDriverRealSummaryForm);
            HashMap summaryMap = (HashMap) r.get("result");

            //获取私有读写文件的临时URL地址
            String idcardFront = MapUtil.getStr(summaryMap, "idcardFront");
            String idcardBack = MapUtil.getStr(summaryMap, "idcardBack");
            String idcardHolding = MapUtil.getStr(summaryMap, "idcardHolding");
            String drcardFront = MapUtil.getStr(summaryMap, "drcardFront");
            String drcardBack = MapUtil.getStr(summaryMap, "drcardBack");
            String drcardHolding = MapUtil.getStr(summaryMap, "drcardHolding");
            idcardFront = idcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(idcardFront) : "";
            idcardBack = idcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(idcardBack) : "";
            idcardHolding = idcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(idcardHolding) : "";
            drcardFront = drcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(drcardFront) : "";
            drcardBack = drcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(drcardBack) : "";
            drcardHolding = drcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(drcardHolding) : "";
            summaryMap.replace("idcardFront", idcardFront);
            summaryMap.replace("idcardBack", idcardBack);
            summaryMap.replace("idcardHolding", idcardHolding);
            summaryMap.replace("drcardFront", drcardFront);
            summaryMap.replace("drcardBack", drcardBack);
            summaryMap.replace("drcardHolding", drcardHolding);
            //TODO 这里以后还有很多要写的东西
            SearchDriverMonthBusinessDataForm searchDriverMonthBusinessDataForm = new SearchDriverMonthBusinessDataForm();
            searchDriverMonthBusinessDataForm.setDriverId(driverId);
            //查询好评差评
            R r1 = odrServiceApi.searchDriverMonthByPositiveAndNegativeReviews(searchDriverMonthBusinessDataForm);
            HashMap result1 = (HashMap) r1.get("result");
            Integer rate = MapUtil.getInt(result1, "rate") != null ? MapUtil.getInt(result1, "rate") : 0;
            String remark = MapUtil.getStr(result1, "remark") != null ? MapUtil.getStr(result1, "remark") : "";
            //查询当月订单金额、订单数。
            R r2 = odrServiceApi.searchDriverYearBusinessMonth(searchDriverMonthBusinessDataForm);
            HashMap result2 = (HashMap) r2.get("result");
            String income = MapUtil.getStr(result2, "income");
            String orders = MapUtil.getStr(result2, "orders");
            //月订单分账,代扣个税
            R r3 = odrServiceApi.searchDriverYearByMonthlyOrderSplittingPersonalIncomeTax(searchDriverMonthBusinessDataForm);
            ArrayList<HashMap> result3 = (ArrayList<HashMap>) r3.get("result");
            Integer driverIncome = result3.size()==0?0:result3.size();
            Integer taxRate =result3.size()==0?0:result3.size();
            summaryMap.put("total", income); //月订单金额
            summaryMap.put("income", driverIncome); //月订单分账
            summaryMap.put("tax", taxRate);//代扣个税
            summaryMap.put("count", orders);//总代驾单数
            summaryMap.put("ct1", rate);//好评总数
            summaryMap.put("ct2", remark);//差评备注
            map.put("summaryMap", summaryMap);
        }
        return map;
    }

    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverRealAuth(UpdateDriverRealAuthForm form) {
        R r = drServiceApi.updateDriverRealAuth(form);
        Integer rows = MapUtil.getInt(r, "rows");
        //TODO 此处应该调用子系统进行通知
        return rows;
    }

}
